package com.jz.beijianbao.beijian.service.impl;

import com.jz.beijianbao.beijian.bean.*;
import com.jz.beijianbao.beijian.mapper.BeiJianMapper;
import com.jz.beijianbao.beijian.personEnum.PersonEnum;
import com.jz.beijianbao.beijian.service.BeiJianService;
import com.jz.beijianbao.beijian.bean.SubClass;
import com.jz.util.AllRecordsVo;
import com.jz.util.ResultBuildVO;
import com.jz.util.ResultVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class BeiJianServiceImpl implements BeiJianService {

    @Autowired
    private BeiJianMapper beiJianMapper;

    @Override
    public ResultVO subClassQuery() {
        List<SubClass> subClasses = beiJianMapper.subClassQuery();

        return ResultBuildVO.success(subClasses);
    }
    @Override
    public ResultVO planClassQueryAll() {
        List<PlanClass> planClasses =  beiJianMapper.planClassQueryAll();

        return ResultBuildVO.success(planClasses);
    }
    @Override
    public ResultVO legalPerson() {
        List<LegalPerson> legalPeople = beiJianMapper.queryPerson();

        return ResultBuildVO.success(legalPeople);
    }


    @Override
    public ResultVO queryPage(BeiJianSelectVo beiJianSelectVo) {
        List<BeiJian> beiJianSelectVo1 = beiJianMapper.queryPage(beiJianSelectVo);
        for (BeiJian plant: beiJianSelectVo1) {
            Integer enabled = plant.getStatus() ? 1:0;
            plant.setStatusName(PersonEnum.getName(enabled));
        }
        int count = beiJianMapper.count(beiJianSelectVo);
        AllRecordsVo allRecordsVo = new AllRecordsVo();
        allRecordsVo.setPageIndex(beiJianSelectVo.getPageIndex());
        allRecordsVo.setPageSize(beiJianSelectVo.getPageSize());
        allRecordsVo.setTotalSize(count);
        allRecordsVo.countPage(count);
        allRecordsVo.setDataList(beiJianSelectVo1);
        return ResultBuildVO.success(allRecordsVo);
    }

    //新增
    @Override
    public ResultVO add(BeiJianAdd beiJianAdd) {
        Date now = new Date();
        BeiJian legalPlantVo1 = new BeiJian();
        BeanUtils.copyProperties(beiJianAdd,legalPlantVo1);
        legalPlantVo1.setCreateName("从session中获取");
        legalPlantVo1.setCreateTime(now);
        beiJianMapper.addPlant(legalPlantVo1);
        beiJianMapper.addBeiJianPerson(beiJianAdd);
        return ResultBuildVO.success();
    }

    @Override
    public ResultVO updatePlant(BeiJianUpdate beiJianUpdate) {
        if (beiJianUpdate.getStatus()){
            return ResultBuildVO.error("只有在禁用状态才能修改");
        }
        Date now = new Date();
        BeiJian legalPlant1 = new BeiJian();
        BeanUtils.copyProperties(beiJianUpdate,legalPlant1);
        legalPlant1.setUpdateName("从session获取");
        legalPlant1.setUpdateTime(now);
        int version = beiJianMapper.updatePlant(legalPlant1);
        if (version==0){
            return ResultBuildVO.error("情刷新后修改");
        }
        return ResultBuildVO.success();
    }

    @Override
    public ResultVO updateStatus(BeiJianUpdatestatus beiJianUpdatestatus) {
        Date now = new Date();
        BeiJian legalPlant1 = new BeiJian();
        BeanUtils.copyProperties(beiJianUpdatestatus,legalPlant1);
        legalPlant1.setUpdateName("从session获取");
        legalPlant1.setUpdateTime(now);
        beiJianMapper.updateStatus(legalPlant1);
        return ResultBuildVO.success();

    }



}
